<!--
* description: 网点备件销售-销售单退货
* author: hzm
* createdDate: 2021-1-27
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-api-config="dynamicApiConfigs"
      :dynamic-table-cols="tableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-show-select="true"
      :dynamic-is-init-table="true"
      :dynamic-is-fix-choose="false"
      :dynamic-is-fix-index="false"
      :dynamic-is-show-more-btn="true"
      :callbackObj="callbackObj"
      :dynamic-api-style="'restful'"
    />
  </div>
</template>

<script>
import { formMixins } from "@/components/mixins/formMixins";
import { oneTableWithViewTemplateMixins } from "@/components/mixins/oneTableWithViewTemplateMixins";
import OneTableTemplate from "@/components/templates/oneTable";
import { orgApis } from "@/api/paSparePartsManagement";

export default {
  name: "salesReturn",
  components: {
    OneTableTemplate,
  },
  mixins: [oneTableWithViewTemplateMixins, formMixins],
  data() {
    return {
      selectiondata: null,
      dynamicApiConfigs: orgApis.querySalesOrderReturn,
      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
              {
                prop: "saleOrderCode",
                label: "销售单号",
                align: "center",
              },
              {
                prop: "customerTypeName",
                label: "客户性质",
                align: "center",
              },
              {
                prop: "saleType",
                label: "销售类型",
                align: "center",
              },
              {
                prop: "customerName",
                label: "客户名称",
                align: "center",
              },
              {
                prop: "customerPhone",
                label: "联系电话",
                align: "center",
              },
              {
                prop: "partNo",
                label: "备件编号",
                align: "center",
              },
              {
                prop: "partName",
                label: "备件名称",
                align: "center",
              },
              {
                prop: "salePrice",
                label: "销售价格",
                align: "center",
              },
              {
                prop: "saleQty",
                label: "销售数量",
                align: "center",
              },
              {
                prop: "saleAmount",
                label: "销售金额",
                align: "center",
              },
              {
                prop: "outboundQty",
                label: "已出库数量",
                align: "center",
              },
              {
                prop: "returnQty",
                label: "退货数量+",
                align: "center",
                isComponent: true,
                comps: [
                  {
                    compKey: "returnQty",
                    codeField: "returnQty",
                    component: () => import("@/components/org/commonInput"),
                  },
                ],
              },
              {
                prop: "saleStatus",
                label: "销售状态",
                align: "center",
              },
              {
                prop: "empName",
                label: "制单人",
                align: "center",
              },
              {
                prop: "createdDate",
                label: "制单时间",
                align: "center",
              },
            ],
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
              .tableComponents
          : [
              // 库存状态
              {
                compKey: "compKey1",
                labelName: "销售单号：",
                codeField: "saleOrderCode",
                lookuptype: "VE0212",
                component: () => import("@/components/org/commonInput"),
                type: "inputText",
                isMust: true,
                span: 6,
              },
              // 车辆状态
              {
                compKey: "compKey2",
                labelName: "客户性质：",
                codeField: "customerType",
                component: () => import("@/components/org/userSeleapiConfig"),
                oFields: "statusId,statusText",
                type: "inputText",
                options: [
                  { statusId: "1", statusText: "直营店" },
                  { statusId: "2", statusText: "授权店" },
                  { statusId: "0", statusText: "普通客户" },
                ],
                isMust: true,
                span: 6,
              },
              // 入库仓库
              {
                compKey: "compKey3",
                labelName: "客户名称：",
                codeField: "customerName",
                component: () => import("@/components/org/commonInput"),
                oFields: "AsId,AsText",
                type: "inputText",
                isMust: true,
                span: 6,
              },
              // 审核状态
              {
                compKey: "compKey4",
                labelName: "联系电话：",
                codeField: "customerPhone",
                component: () => import("@/components/org/commonInput"),
                oFields: "AsId,AsText",
                type: "inputText",
                isMust: true,
                span: 6,
              },
              {
                compKey: "compKey5",
                labelName: "制单日期：",
                codeField: "createdDate",
                component: () =>
                  import("@/components/org/datePicker/andtDatePicker"),
                oFields: "AsId,AsText",
                type: "inputText",
                isMust: false,
                span: 6,
              },
              {
                compKey: "compKey6",
                labelName: "制单人：",
                codeField: "creator",
                lookuptype: "VE0212",
                component: () => import("@/components/org/commonInput"),
                type: "inputText",
                isMust: false,
                span: 6,
              },
              {
                compKey: "compKey7",
                labelName: "销售状态：",
                codeField: "saleStatus",
                lookuptype: "VE0212",
                component: () => import("@/components/org/userSeleapiConfig"),
                type: "inputText",
                oFields: "statusId,statusText",
                options: [
                  { statusId: "1", statusText: "已提交" },
                  { statusId: "2", statusText: "已收款" },
                  { statusId: "3", statusText: "部分出库" },
                  { statusId: "4", statusText: "全部出库" },
                ],
                isMust: false,
                span: 6,
              },
              {
                compKey: "compKey8",
                labelName: "销售类型：",
                codeField: "saleType",
                lookuptype: "VE0212",
                component: () => import("@/components/org/userSeleapiConfig"),
                type: "inputText",
                oFields: "statusId,statusText",
                options: [
                  { statusId: "2", statusText: "常规销售" },
                  { statusId: "0", statusText: "呆滞件销售" },
                ],
                isMust: false,
                span: 6,
              },
            ],
      tableButtons: [
        {
          conmpKey: "btenKey3",
          type: "primary",
          size: "small",
          text: "查询",
          name: "search",
          clickEvent: () => this.queryTable(1),
          position: "right",
        },
        {
          conmpKey: "btenKey1",
          type: "",
          size: "small",
          text: "退货",
          name: "save",
          clickEvent: () => this.save(),
          position: "right",
        },
        {
          conmpKey: "btenKey2",
          type: "",
          size: "small",
          text: "重置",
          name: "reset",
          clickEvent: () => this.reset(),
          position: "right",
        },
      ],
      formField: this.$utils.getFormField(this, {
        saleOrderCode: "",
        customerType: "",
        customerName: "",
        customerPhone: "",
        creator: "",
        saleType: "",
        saleStatus: "",
        createdDate: "",
      }),
      callbackObj: {
        // 手动勾选网格时，拿到勾选网格的数据
        onCheckSelect: (selection, row) => {
          console.log(selection, row, "111");
          this.selectiondata = selection;
        },
      },
    };
  },
  methods: {
    // 退货
    save() {
      let that = this;
      let infoQue = {backList:[]}
      let saveType = false
      // let infoQue = []
      if( !this.selectiondata ){
        that.$message({
            message: "请勾选数据",
            type: "warning",
            duration: 2000,
          });
        return
      }      
      this.selectiondata.forEach(item=>{
        if( !item.returnQty || Number(item.returnQty) > item.saleQty){
          saveType = true 
        }
      })
      if(saveType){
        that.$message({
            message: "请填写正确的退货数量",
            type: "warning",
          });
        return
      }
      this.selectiondata.forEach(function (val, index) {
        infoQue.backList.push({
          returnQty: val.returnQty,
          saleQty: val.saleQty,
          salePrice: val.salePrice,
          saleOrderDId: val.saleOrderDId,
          receivableAmount: val.receivableAmount,
          saleAmount: val.saleAmount,
          saleOrderId: val.saleOrderId,
          updateControlId: val.updateControlId
        });
      });
      let queryObj = {
        apiConfig: orgApis.returnEdit,
        //条件/实体参数（变量），根据typeParam中的定义配置
        variables: {
          //当前中台使用的名称有dataInfo、info，具体查看API文档
          dataInfo: infoQue,
        },
      };
      //转换了中台请求格式数据
      var paramD = that.$getParams(queryObj);
      // 调用中台API方法（可复用）
      that.$requestAPI(paramD).then((response) => {
        if (response.result === "0") {
          return;
        }
        var getResult = response;
        if (getResult.result === "1") {
          that.$message({
            message: "退货成功",
            type: "success",
            duration: 2000,
          });
          that.queryTable(1)
          // 重置表单
          // that.resetRight(tableRef)
          // 重新加载
          // that.onLeftRowClick(null);
        } else {
          that.$message({
            message: getResult.msg,
            type: "warning",
            duration: 2000,
          });
        }
      });
      // }
    },
  },
  mounted() {
    // this.queryTable(1)
    this.$refs.multipleTable.list = [
      {
        index: 1,
      },
    ];
  },
};
</script>

<style>
</style>
